package controllers;

import dao.*;
import play.data.FormFactory;
import play.libs.Json;
import play.libs.concurrent.HttpExecutionContext;
import play.mvc.Controller;
import play.mvc.Result;

import javax.inject.Inject;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletionStage;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;


/**
 * The controller keeps all database operations behind the repository, and uses
 * {@link play.libs.concurrent.HttpExecutionContext} to provide access to the
 * {@link play.mvc.Http.Context} methods like {@code request()} and {@code flash()}.
 */
public class WeigherController extends Controller {

    private final FormFactory formFactory;
    private final WeigherInfo_Repository weigherInfo_repository;
    private final HttpExecutionContext ec;

    @Inject
    public WeigherController(FormFactory formFactory, WeigherInfo_Repository weigherInfo_repository, HttpExecutionContext ec) {
        this.formFactory = formFactory;
        this.weigherInfo_repository = weigherInfo_repository;
        this.ec = ec;
    }

    public CompletionStage<Result> getWeigher() {
        JsonNode json = request().body().asJson();
        String PlaceName = json.findPath("PlaceName").textValue();
        String TranType = json.findPath("TranType").textValue();
        String ScaleNo = json.findPath("ScaleNo").textValue();

        return weigherInfo_repository.getWeigher(PlaceName, TranType, ScaleNo).thenApplyAsync(_GetListByFunc -> {
            return ok(Json.toJson(_GetListByFunc));
        }, ec.current());
    }

}